
// 1. 计算数组中的每个值  求和
function fn(arr: number[]): number {
    let sum: number = 0
    arr.forEach((item) => {
        sum += item
    })
    return sum
}
console.log(fn([2, 4, 6, 7, 9]));


// 2. 二维数组转一维  [1,2,3,4,5]
function fn1(arr1: Array<number | Array<number>>): Array<number> {
    let newArr1: Array<number> = []
    arr1.forEach((item: Array<number> | number) => {
        if (Array.isArray(item)) {
            newArr1.push(...fn1(item))
        }
        else {
            newArr1.push(item)
        }
    })
    return newArr1
}
console.log(fn1([[2, 4], [5, 6, 7], 8]));


// 3. 冒泡排序
function my_sort(arr2: number[]): Array<number> {
    // let length=arr2.length
    // if (length <= 1) {
    //     return arr2
    // } else {
    //     for (let i = 0; i < length; i++) {
    //         for (let j = i + 1; j < length; j++) {
    //             if (arr2[j]<arr2[i]) {
    //               let temp=arr2[j]
    //               arr2[j]=arr2[i]
    //               arr2[i]=temp
    //             }
    //         }
    //     }
    // }
    for (let i = 0; i < arr2.length - 1; i++) {
        for (let j = 0; j < arr2.length - 1 - i; j++) {
            if (arr2[j] > arr2[j + 1]) {
                let temp = arr2[j]
                arr2[j] = arr2[j + 1]
                arr2[j + 1] = temp
            }
        }
    }

    return arr2
}
console.log(my_sort([5, 6, 2, 78, 1]));


// 4. 菲波纳锲  1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
function up(n: number): Array<number> {
    let arr: Array<number> = [1, 1]
    if (n <= 2) {
        return arr
    } else {
        for (let i = 2; i < n; i++) {
            arr[i] = arr[i - 1] + arr[i - 2]
        }
        return arr
    }
}
console.log(up(10));


// 5. 模拟push 方法  [1,2,3,"上海",4,5,6]
function my_push(arrs: Array<number | string>, ...rest: number[]): Array<number | string> {
    let newarrs = arrs
    for (let i = 0; i < rest.length; i++) {
        newarrs[newarrs.length] = rest[i]
    }
    return arrs
}
console.log(my_push([1, 2, 3, "上海"], 4, 5, 6));


// 6. 模拟pop 数组删除最后一项 [1,2]
function del(arrpop: any): any {
    arrpop.length = arrpop.length - 1
    return arrpop
}
console.log(del([2, 5, null, "见到过", 9]));


// 7. 补零 将我们数组中所有数字 个位数 前面加0 [01,02,03,11,12]
function pu(brr: number[]): any {
    let newBrr: Array<number | string> = []
    for (let i = 0; i < brr.length; i++) {
        if (brr[i] < 10) {
            newBrr.push('0' + brr[i])
        }
        else {
            newBrr.push(brr[i])
        }
    }
    return newBrr
}
console.log(pu([1, 2, 3, 11, 12]));


// 8. 求最大值 100 最小值
function max(z: number[]): number {
    let zs = z.sort((a, b) => b - a)[0]
    return zs
}
console.log(max([4, 100, 3, 48, 90]));


// 9. 反转字符串  abc => cba
function rever(json: string): string {
    return json.split('').reverse().join('')
}
console.log(rever("abc"));


// 10. 找最长单词  ["aaaaaaa","aaa","b"]  => "aaaaa"
function lengths(crr: string[]): string {
    let crrs: string = crr.sort((a: string, b: string) => {
        return b.length - a.length
    })[0]
    return crrs

}
console.log(lengths(['dddddddddddd', "fffff", "iii"]));

// 11. 转驼峰  hello world jiang guo qing helloWorldJiangGuoQing
function fn6(drr:string):string {
    let drrs = drr.split(' ')
    let newDrrs = drrs[0]
    for (let i = 1; i < drrs.length; i++) {
        newDrrs += drrs[i][0].toUpperCase() + drrs[i].slice(1) + ' '
    }
    return newDrrs

}
console.log(fn6(' hello word is qing long'));

// function fn5(str:string):string {
//     let strs=str.split(' ')
//     let newStrs=strs[0]
//    for (let i = 1; i < strs.length; i++) {
//       newStrs+=strs[i][0].toUpperCase()+strs[i].slice(1)+' '
//    }
//    return newStrs
// }
// console.log(fn5(' hello word is qian wang tiao'));

// 12. 封装find
var arr7 = [
    { name: 'chen', age: 18 },
    { name: 'li', age: 10 },
    { name: 'li', age: 13 },
    { name: 'huang', age: 44 },
    { name: 'zhou', age: 100 },
]

interface obj {
    name:string,
    age:number
}

function my_find(arr7:Array<obj>,nameorage:string|number):any{
    for (let i = 0; i < arr7.length; i++) {
        if( arr7[i].name === nameorage || arr7[i].age === nameorage){
            return arr7[i]
        }
        
    }[0]
}

console.log(my_find(arr7,44));
// 13. 封装reducer
var arr1  = [1, 2, 3, 4];

function my_redcer(arr1:number[],sum:number = 0):number{
    console.log(arr1,sum);
    arr1.forEach(item=>{
        sum += item
    })
    return sum
    
}

console.log(my_redcer(arr1,10));
// 14. 去重
function my_newSet(arr:number[]):number[]{
    // let data:number[] = []
    // data = [...new Set(arr)]
    for (let i = 0; i < arr.length; i++) {
        for (let j = i+1; j < arr.length-1; j++) {
                if (arr[i] === arr[j]) {
                    arr.splice(j,1)
                    j--
                }
        }
    }
    return arr
}
console.log(my_newSet([1,1,1,1,2,3,4]));
// 15. 一维转二维
function fnp(arr:Array<number>,num:number):Array<number|Array<number>>{
    let newArr:Array<number|Array<number>> = []

    while(arr.length>0){
        newArr.push(arr.splice(0,num))
    }
    return newArr

}

console.log(fnp([1,2,3,4,5,6,7,8,9],4));